package com.xbongbong.paas.lowcode;

import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.pojo.dto.FormDataAddDTO;
import com.xbongbong.paas.pojo.dto.FormDataUpdateDTO;
import com.xbongbong.paas.abstracts.AbstractLowCodeExecuteService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.pro.enums.lowcode.LowCodeExecuteServiceEnum;
import com.xbongbong.pro.formdata.pojo.vo.BeforeSaveVO;
import com.xbongbong.pro.formdata.pojo.vo.BeforeUpdateVO;
import com.xbongbong.pro.formdata.pojo.vo.SaveVO;
import com.xbongbong.pro.formdata.pojo.vo.UpdateVO;
import com.xbongbong.pro.lowcodeexecute.pojo.dto.LowCodeExecuteServiceDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 吴峰
 * @date 2022/01/05 11:06
 */
@Component
public class LowCodeExecuteHelp {

    private static final Logger LOG = LoggerFactory.getLogger(LowCodeExecuteHelp.class);

    @Resource
    private AbstractLowCodeExecuteService abstractLowCodeExecuteService;

    /**
     * 低代码执行服务
     * @param formDataAddDTO
     * @param saveVO
     * @param beforeSaveVO
     * @throws XbbException
     */
    public void lowCodeExecuteService(FormDataAddDTO formDataAddDTO, SaveVO saveVO, BeforeSaveVO beforeSaveVO) throws XbbException {
        try {
            LowCodeExecuteServiceDTO lowCodeExecuteServiceDTO = new LowCodeExecuteServiceDTO();
            lowCodeExecuteServiceDTO.setAlias(LowCodeExecuteServiceEnum.ADD.name());
            lowCodeExecuteServiceDTO.setBusinessType(formDataAddDTO.getBusinessType());
            lowCodeExecuteServiceDTO.setDataId(Arrays.asList(saveVO.getDataId()));
            lowCodeExecuteServiceDTO.setFormId(formDataAddDTO.getFormId());
            lowCodeExecuteServiceDTO.setSaasMark(formDataAddDTO.getSaasMark());
            lowCodeExecuteServiceDTO.setCorpid(formDataAddDTO.getCorpid());
            List<PaasFormDataEntityExt> list = new ArrayList<>();
            PaasFormDataEntityExt paasFormDataEntityExt = new PaasFormDataEntityExt();
            BeanUtil.copyProperties(saveVO.getPaasFormDataEntity(), paasFormDataEntityExt);
            list.add(paasFormDataEntityExt);
            lowCodeExecuteServiceDTO.setList(list);
            abstractLowCodeExecuteService.execute(lowCodeExecuteServiceDTO);
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.impl.AbstractFormDataServiceImpl.lowCodeExecuteService fail", e);
        }

    }

    /**
     * 低代码执行服务
     * @param formDataUpdateDTO
     * @param updateVO
     * @param beforeUpdateVO
     * @throws XbbException
     */
    public void lowCodeExecuteService(FormDataUpdateDTO formDataUpdateDTO, UpdateVO updateVO, BeforeUpdateVO beforeUpdateVO) throws XbbException {
        try {
            LowCodeExecuteServiceDTO lowCodeExecuteServiceDTO = new LowCodeExecuteServiceDTO();
            lowCodeExecuteServiceDTO.setAlias(LowCodeExecuteServiceEnum.EDIT.name());
            lowCodeExecuteServiceDTO.setBusinessType(formDataUpdateDTO.getBusinessType());
            lowCodeExecuteServiceDTO.setFormId(formDataUpdateDTO.getFormId());
            lowCodeExecuteServiceDTO.setDataId(Arrays.asList(updateVO.getDataId()));
            lowCodeExecuteServiceDTO.setCorpid(formDataUpdateDTO.getCorpid());
            lowCodeExecuteServiceDTO.setSaasMark(formDataUpdateDTO.getSaasMark());
            PaasFormDataEntityExt paasFormDataEntityExt = new PaasFormDataEntityExt();
            BeanUtil.copyProperties(updateVO.getPaasFormDataEntity(), paasFormDataEntityExt);
            List<PaasFormDataEntityExt> list = new ArrayList<>();
            list.add(paasFormDataEntityExt);
            lowCodeExecuteServiceDTO.setList(list);
            abstractLowCodeExecuteService.execute(lowCodeExecuteServiceDTO);
        } catch (Exception e) {
            LOG.error("com.xbongbong.paas.service.impl.AbstractFormDataUpdateServiceImpl.lowCodeExecuteService fail", e);
        }

    }
}
